//Create PickerTextField
function PickerTextField(inView, type, pkData) {
    var osname = Ti.Platform.osname;
    var self = null;
    if (osname === 'android') {
        // Create a Picker.
        self = Ti.UI.createPicker({
            selectionIndicator: true,
            textAlign: 'center'
        });
        if (type === 1) {
            // Add data to the Picker.
            var data = [];
            for (var i = 0; i < pkData.length; i++) {
                data[i] = Ti.UI.createPickerRow({
                    title: pkData[i].title,
                    valueId: pkData[i].valueId
                });
            };
            self.add(data);
            // Listen for change events.
            self.addEventListener('change', function (e) {
                self.fireEvent('pickerTextFieldChange', {
                    valueId: pkData[e.rowIndex].valueId
                });
            });
            self.fireEvent('pickerTextFieldChange', {
                valueId: pkData[0].valueId
            });
            self.addEventListener('changeIndexSelected', function (e) {
                Ti.API.info('changeIndexSelected ' + e.indexSelected);
                self.setSelectedRow(0, e.indexSelected, false);
            });
        } else {
            self.setType = Titanium.UI.PICKER_TYPE_DATE;
            self.addEventListener('change', function (e) {
                self.fireEvent('pickerTextFieldDateChange', {
                    valueId: pkData[e.rowIndex].valueId
                });
            });
        };
    } else {
        // Create a TextField.
        self = Ti.UI.createTextField({
            borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
            textAlign: 'center'
        });
        // Listen for return events.
        self.addEventListener('return', function (e) {
            Ti.API.info('Picker return');
            self.blur();
        });
        self.addEventListener('click', function (e) {
            Ti.API.info('Picker return');
        });
        self.addEventListener('focus', function (e) {
            Ti.API.info('Picker click');
            self.blur();
            // Create a Picker.
            aPicker = Ti.UI.createPicker({
                selectionIndicator: true
            });
            if (type === 1) {
                var data = [];
                for (var i = 0; i < pkData.length; i++) {
                    data[i] = Ti.UI.createPickerRow({
                        title: pkData[i].title,
                        valueId: pkData[i].valueId
                    });
                };
                aPicker.add(data);
                // Listen for change events.
                aPicker.addEventListener('change', function (e) {
                    self.value = pkData[e.rowIndex].title;
                    self.fireEvent('pickerTextFieldChange', {
                        valueId: pkData[e.rowIndex].valueId
                    });
                });
            } else {
                aPicker.setType = Titanium.UI.PICKER_TYPE_DATE;
                aPicker.addEventListener('change', function (e) {
                    aPicker.fireEvent('pickerTextFieldDateChange', {
                        valueId: pkData[e.rowIndex].valueId
                    });
                });
            }
            // Add to the parent view.
            fakeView = Titanium.UI.createView({
                backgroundColor: 'transparent',
                top: 0,
                left: 0,
                width: inView.width,
                height: inView.height
            });
            inView.add(fakeView);
            fakeView.addEventListener('click', function (e) {
                inView.remove(fakeView);
                inView.remove(aPicker);
            });
            inView.add(aPicker);
            aPicker.bottom = 0;
        });
        if (type === 1) {
            self.value = pkData[0].title;
            self.fireEvent('pickerTextFieldChange', {
                valueId: pkData[0].valueId
            });
        };
        self.addEventListener('closeKeyBoard', function (e) {
            inView.remove(fakeView);
            inView.remove(aPicker);
        });
        self.addEventListener('changeIndexSelected', function (e) {
            Ti.API.info('changeIndexSelected ' + e.indexSelected);
            self.value = pkData[e.indexSelected].title;
        });
    };
    var fakeView = null;
    var aPicker = null;
    return self;
}
module.exports = PickerTextField;